#!/bin/bash
#ecocycData="ecoli.profil"
#ecocycPath="./ecoli/ecocyc/"
#working_directory="./example/"

ecocycData="ATCC23270.profil"
ecocycPath="./data_sources/16.1/afer243159/"
working_directory="./afer_ATCC23270/"

model_profil="${working_directory}/modele.profil"
R_directory="./R/"



#Gint creation parameter
metabolite_cutoff="40"

#k-SIP parameter
k="5"

geneDistance="10"
threads="2"

libs_dir="./libs"
java_cp="$libs_dir/jgrapht-jdk1.6.jar:$libs_dir/colt-1.2.0.jar"
#java_cp="$libs_dir/jgrapht-jdk1.6.jar:$libs_dir/colt-1.2.0.jar:$libs_dir/collections-generic-4.01.jar"
#-------------------------------------------#
#                "Gint Creation"            #
#-------------------------------------------#

echo "Gint Creation"
#Creating Gint called "modele.txt" in a $working_directory from ecocyc flat-file described in the profile $ecocycData. Metabolites that appreas in more than $metabolite_cutoff reactions are removed. The Gint metric is the colocalization distance.
java -classpath sipper.jar:"$java_cp" combi.wip.sipper.exec.CreationGintFromEcocyc "$ecocycData" "$working_directory" "$metabolite_cutoff"

#Creating Gint called "modele.txt" in a $working_directory from ecocyc flat-file described in the profile $ecocycData. Metabolites that appreas in more than $metabolite_cutoff reactions are removed. The Gint metric is coded into a NxN matrice file with the following this format:
#					"gene_1_id	gene_2_id	...	gene_N_id"
#					"val_1_1	val_1_2	... val_1_N"
#					"val_2_1	val_2_2	... val_2_N"
#					"..."
#					"val_N_1	val_N_2	... val_N_N"
#
# The first line list the gene id of each gene
# val_i_j code for the metric from gene_i_id to gene_j_id.
# Each id or value is separate from other by a tabulation

#java -classpath sipper.jar:"$java_cp" sipper.CreationGintFromEcocyc "$ecocycData" "$working_directory" "$metabolite_cutoff" "$matrix"



input="${working_directory}/modele.txt"
reaction_list="${working_directory}/reaction_list.txt"
reaction_list_queries="${working_directory}/reaction_list_queries.txt"
queries="${working_directory}/queries_${geneDistance}_genes.txt"
genome_biosp="${working_directory}/genome.txt"
metabolism_bioasp="${working_directory}/metabolism.txt"
catalyze="${working_directory}/catalyze.txt"
association_bioasp="${working_directory}/catalyze_inv.txt"
assoReact="${working_directory}/associationsR.txt"
output="${working_directory}/result_${k}sips.txt"
output_filtered_1="${working_directory}/result_${k}sips_filtered_w.txt"
output_filtered="${working_directory}/result_${k}sips_filtered_dG.txt"
output_filtered_gene_set="${working_directory}/result_${k}sips_filtered_dG_gene_set.txt"
output_filtered_reaction_set="${working_directory}/result_${k}sips_filtered_dG_reaction_set.txt"
output_bioasp="${working_directory}/result_${k}sips_bioasp.txt"
output_bioasp_trad="${working_directory}/result_${k}_bioasp_trad.txt"
labels="${working_directory}/labels.txt" 
output_filtered_trad="${working_directory}/result_${k}sips_filtered_trad.txt"
diff_output="${working_directory}/compare_result_${k}sips_filtered_trad.txt"

R_program="${R_directory}/compareSets.R"
R_output="${working_directory}/Routput.txt"
operons_listing="./operonsColi.txt"
similarity_operons="${working_directory}/result_${k}sips_filtered_dG_gene_set_VS_operonColi.txt"

ksip_inclusion_treilli="${working_directory}/result_${k}sips_filtered_dG_trad_treilli.dot"
ksip_inclusion_leaves="${working_directory}/result_${k}sips_filtered_dG_trad_leaves.txt"
ksip_inclusion_treilli_colored="${working_directory}/result_5sips_filtered_dG_trad_treilli_colored_with_operons.dot"
ksips_modules_output_rep="${working_directory}/result_${k}sips_filtered_dG_trad_modules/"


#java_cp="$libs_dir/jgrapht-jdk1.6.jar:$libs_dir/colt-1.2.0.jar:$libs_dir/collections-generic-4.01.jar"
#-------------------------------------------#
#        "Reaction list generation"         #
#-------------------------------------------#
echo ""
echo "Reaction list generation"
#awk '{print $1}' "$assoReact" > "$reaction_list"
python ./queries_clean.py "$metabolism_bioasp" > "$reaction_list_queries"
#permut columns
awk '{print $2 "\t" $1}' "$catalyze" > "$association_bioasp"

echo ""
echo "k-SIPs queries generation"
time java -Xss1024m -Xms512m -Xmx2048m -classpath sipper.jar combi.wip.sipper.exec.GenerateKSIPsEndPoints l "$reaction_list_queries" "$queries" "geneDistance=$geneDistance" "${ecocycPath}/genes.dat" "$association_bioasp" "${ecocycPath}/proteins.dat"



#-------------------------------------------#
#           "k-SIPs Computation"            #
#-------------------------------------------#

echo "k-SIPs Computation - dedicated"
#Creating a list of kSIPs queries as a list of couple of start -> end reaction with some filtering options.
#java -classpath sipper.jar:"$java_cp" combi.wip.sipper.exec.GenerateKSIPsEndPoints l|i fileIn fileOut none|geneDistance=XX|reactionDistance=XX
# - The first parameter is "l" or "i" and defines the form of the entry file. The parameter "l" means the entry file is a set of reaction and all possible couples from this set will be generate. The parameter "i" indicate that the list is already under the form r1 -> r2 and no more couples will be generate.
# - none means no filtering, geneDistance=XX keep only reactions that are distant at most XX genes (i.e. in an interval of x+1 genes), end reactionDistance=XX  keep only reaction that are distant at most XX arcs in the graph reaction.
time java -Xss1024m -Xms512m -Xmx2048m -classpath sipper.jar:"$java_cp" combi.wip.sipper.exec.ComputeKSIPs "$input" "$queries" "$assoReact" "$k" "$output" "$threads"


echo "Filtering k-SIPs (weight <= 300)"
time java -classpath sipper.jar:"$java_cp" combi.wip.sipper.exec.ExtractSimple "$output" "$output_filtered_1" "$ecocycData" "$model_profil" "none" "w=300"

echo "Filtering k-SIPs again (density >= 0.7)"
time java -classpath sipper.jar:"$java_cp" combi.wip.sipper.exec.ExtractSimple "$output_filtered_1" "$output_filtered" "$ecocycData" "$model_profil" "bestSubset" "dG=0.7"

echo "Translating k-SIPs"
time java -classpath sipper.jar combi.wip.sipper.exec.TransSimple "$output_filtered" "$labels" "$output_filtered_trad"

#echo "k-SIPs Computation - bioASP"
#time python2.7 finder_ksip.py "$genome_biosp" "$metabolism_bioasp" "$association_bioasp" "$k" "$queries" > "$output_bioasp"

#echo "Translating k-SIPs - bioASP"
#time java -classpath sipper.jar combi.wip.sipper.io.IObioASPkSIPs "$output_bioasp" "$output_bioasp_trad"

#-------------------------------------------#
#            "k-SIPs Comparison"            #
#-------------------------------------------#

#echo "Comparing k-SIPs from dedicated and bioASP approaches"
#java -classpath sipper.jar combi.wip.sipper.exec.CompareSimple "$output_filtered_trad" "$output_bioasp_trad" "$diff_output"

echo "Comparing k-SIPs with operons"
java -classpath sipper.jar combi/wip/sipper/exec/ExtractSetOfEntities 1 < "$output_filtered" > "$output_filtered_gene_set"

java -classpath sipper.jar combi/wip/sipper/exec/ExtractSetOfEntities 2 < "$output_filtered" > "$output_filtered_reaction_set"

R CMD BATCH --no-save --no-restore '--args file1=$output_filtered_gene_set file2=$operons_listing fileOut=$similarity_operons similarityFunc="Jaccard" threshold=0.7' "$R_program" "$R_output"

#-------------------------------------------#
#              "k-SIPs Modules"             #
#-------------------------------------------#

echo "k-SIPs modules Computation"
java -classpath sipper.jar:"$java_cp" combi.wip.sipper.modules.InclusionChemins "$output_filtered_trad" "$ksip_inclusion_treilli" "$ksip_inclusion_leaves"

java -classpath sipper.jar:"$java_cp" combi.wip.sipper.modules.ColoreInclusionChemin "$ksip_inclusion_treilli" "$similarity_operons" "$ksip_inclusion_treilli_colored"

java -classpath sipper.jar:"$java_cp" combi.wip.sipper.modules.GroupeFeuilles2 "$ksip_inclusion_leaves" "$ksips_modules_output_rep" "${ecocycPath}/reactions.dat" "${ecocycPath}/compounds.dat" "$metabolite_cutoff"
